package test.luin.pers.myapplication;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class PersonProvideActivity extends AppCompatActivity implements View.OnClickListener {

    private ContentResolver resolver;
    private ContentValues values;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_provide);
        initView();
        createDB();
    }

    public void initView() {
        findViewById(R.id.btn_select).setOnClickListener(this);
        findViewById(R.id.btn_insert).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_update).setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        resolver = getContentResolver();
        values = new ContentValues();
        uri = Uri.parse("content://test.luin.pers.myapplication/info");
        switch (view.getId()) {
            case R.id.btn_insert:
                Random random = new Random();
                values = new ContentValues();
                values.put("name", "add_itcast" + random.nextInt(10));
                Uri newUri = resolver.insert(uri, values);
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                Log.i("数据库应用", "添加" + newUri);
                break;
            case R.id.btn_delete:
                int row = resolver.delete(uri, "name=?", new String[]{"itcast0"});
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                Log.i("数据库应用", "删除" + row + "条数据");
                break;
            case R.id.btn_update:
                values = new ContentValues();
                values.put("name", "update_itcast");
                int updateRow = resolver.update(uri, values, "name=?", new String[]{"itcast1"});
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
                Log.i("数据库应用", "更新了" + updateRow + "条数据");
                break;
            case R.id.btn_select:
                List<Map<String, String>> mapList = new ArrayList<>();
                Cursor cursor = resolver.query(uri, new String[]{"_id", "name"}, null, null, null, null);
                while (cursor.moveToNext()) {
                    Map<String, String> map = new ArrayMap<>();
                    map.put("_id", cursor.getString(0));
                    map.put("name", cursor.getString(1));
                    mapList.add(map);
                }
                cursor.close();
                Toast.makeText(this, "查找成功", Toast.LENGTH_SHORT).show();
                Log.i("数据库应用", "查询结果:" + mapList.toString());
                break;
        }
    }

    void createDB() {
        PersonDBHelper helper = new PersonDBHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        for (int i = 0; i < 3; i++) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("name", "itcast" + i);
            db.insert("info", null, contentValues);
        }
        db.close();
    }
}
